Add control action types "enable" and "disable".

Akinori MUSHA 10 years ago
parent
commit
a4795734b7

+ 19 - 1
app/concerns/agent_controller_concern.rb

@@ -1,6 +1,10 @@
1 1
 module AgentControllerConcern
2 2
   extend ActiveSupport::Concern
3 3
 
4
+  included do
5
+    validate :validate_control_action
6
+  end
7
+
4 8
   def default_options
5 9
     {
6 10
       'action' => 'run',
@@ -8,7 +12,15 @@ module AgentControllerConcern
8 12
   end
9 13
 
10 14
   def control_action
11
-    options['action'] || 'run'
15
+    options['action'].presence || 'run'
16
+  end
17
+
18
+  def validate_control_action
19
+    case control_action
20
+    when 'run', 'enable', 'disable'
21
+    else
22
+      errors.add(:base, 'invalid action')
23
+    end
12 24
   end
13 25
 
14 26
   def control_targets!
@@ -18,6 +30,12 @@ module AgentControllerConcern
18 30
         when 'run'
19 31
           log "Agent run queued for '#{target.name}'"
20 32
           Agent.async_check(target.id)
33
+        when 'enable'
34
+          log "Enabling the Agent '#{target.name}'"
35
+          target.update!(disable: false) if target.disabled?
36
+        when 'disable'
37
+          log "Disabling the Agent '#{target.name}'"
38
+          target.update!(disable: true) unless target.disabled?
21 39
         end
22 40
       rescue => e
23 41
         log "Failed to #{control_action} '#{target.name}': #{e.message}"

+ 11 - 1
app/models/agents/scheduler_agent.rb

@@ -13,7 +13,17 @@ module Agents
13 13
     cattr_reader :second_precision_enabled
14 14
 
15 15
     description <<-MD % { seconds: (<<-MD_SECONDS if second_precision_enabled) }
16
-      This agent periodically triggers a run of each target Agent according to a user-defined schedule.
16
+      This agent periodically takes an action on target Agents according to a user-defined schedule.
17
+
18
+      # Action types
19
+
20
+      Set `action` to one of the action types below:
21
+
22
+      * `run`: This is the default.  Target Agents are run at intervals.
23
+
24
+      * `disable`: Target Agents are disabled (if not) at intervals.
25
+
26
+      * `enable`: Target Agents are enabled (if not) at intervals.
17 27
 
18 28
       # Targets
19 29
 

+ 1 - 1
app/views/agents/_form.html.erb

@@ -44,7 +44,7 @@
44 44
             <div class="can-be-scheduled">
45 45
               <div class="form-group">
46 46
                 <%= f.label :controllers %>
47
-                <span class="glyphicon glyphicon-question-sign hover-help" data-content="Other than the system-defined schedule above, this agent may be run by user-defined Agents."></span>
47
+                <span class="glyphicon glyphicon-question-sign hover-help" data-content="Other than the system-defined schedule above, this agent may be run or controlled by user-defined Agents."></span>
48 48
                 <% eventControllers = current_user.agents.select(&:can_control_other_agents?) %>
49 49
                 <%= f.select(:controller_ids,
50 50
                              options_for_select(eventControllers.map {|s| [s.name, s.id] },

+ 39 - 1
spec/models/agents/scheduler_agent_spec.rb

@@ -8,6 +8,18 @@ describe Agents::SchedulerAgent do
8 8
   end
9 9
 
10 10
   describe "validation" do
11
+    it "should validate action" do
12
+      ['run', 'enable', 'disable', '', nil].each { |action|
13
+        @agent.options['action'] = action
14
+        @agent.should be_valid
15
+      }
16
+
17
+      ['delete', 1, true].each { |action|
18
+        @agent.options['action'] = action
19
+        @agent.should_not be_valid
20
+      }
21
+    end
22
+
11 23
     it "should validate schedule" do
12 24
       @agent.should be_valid
13 25
 
@@ -45,6 +57,20 @@ describe Agents::SchedulerAgent do
45 57
     end
46 58
   end
47 59
 
60
+  describe 'control_action' do
61
+    it "should be one of the supported values" do
62
+      ['run', '', nil].each { |action|
63
+        @agent.options['action'] = action
64
+        @agent.control_action.should == 'run'
65
+      }
66
+
67
+      ['enable', 'disable'].each { |action|
68
+        @agent.options['action'] = action
69
+        @agent.control_action.should == action
70
+      }
71
+    end
72
+  end
73
+
48 74
   describe "save" do
49 75
     it "should delete memory['scheduled_at'] if and only if options is changed" do
50 76
       time = Time.now.to_i
@@ -62,7 +88,7 @@ describe Agents::SchedulerAgent do
62 88
   end
63 89
 
64 90
   describe "check!" do
65
-    it "should run targets" do
91
+    it "should control targets" do
66 92
       targets = [agents(:bob_website_agent), agents(:bob_weather_agent)]
67 93
       @agent.targets = targets
68 94
       @agent.save!
@@ -74,6 +100,18 @@ describe Agents::SchedulerAgent do
74 100
 
75 101
       @agent.check!
76 102
       target_ids.should be_empty
103
+
104
+      @agent.options['action'] = 'disable'
105
+      @agent.save!
106
+
107
+      @agent.check!
108
+      targets.all? { |target| target.disabled? }
109
+
110
+      @agent.options['action'] = 'enable'
111
+      @agent.save!
112
+
113
+      @agent.check!
114
+      targets.all? { |target| !target.disabled? }
77 115
     end
78 116
   end
79 117
 end